package com.example.JavaConcurrent.Project2_CountDownLatchAndCyclicBarrier.CyclicBarrierBegin_4;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class MyThread  extends Thread{
    private CyclicBarrier cbRef;

    public MyThread(CyclicBarrier cbRef){
        super();
        this.cbRef = cbRef;
    }

    @Override
    public void run() {
        try{
            long sleep = (int)(Math.random()*10000);
            Thread.sleep(sleep);
            System.out.println(Thread.currentThread().getName() + " begin1-Time="+System.currentTimeMillis());
            cbRef.await();
            System.out.println(Thread.currentThread().getName() + " end1-Time="+System.currentTimeMillis());


            sleep = (int)(Math.random()*10000);
            Thread.sleep(sleep);
            System.out.println(Thread.currentThread().getName() + " begin2-Time="+System.currentTimeMillis());
            cbRef.await();
            System.out.println(Thread.currentThread().getName() + " end2-Time="+System.currentTimeMillis());
        }catch (InterruptedException e){

        }catch (BrokenBarrierException e){

        }
    }
}
